package vip.xiaonuo.dev.modular.file.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
 import com.aliyun.sts20150401.Client;
import com.aliyun.sts20150401.models.AssumeRoleRequest;
import com.aliyun.sts20150401.models.AssumeRoleResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.dev.api.DevConfigApi;

@Slf4j
public class DevAliyunVodSTS {


    private static String ALIYUN_ACCESS_KEY_ID_KEY;
    private static String ALIYUN_ACCESS_KEY_SECRET_KEY;
    private static String ALIYUN_ROLE_SESSION_NAME;
    private static String ALIYUN_VOD_ROLE_ARN;
    private static String ALIYUN_VOD_POLICY;
    private static String ALIYUN_VOD_STS_ENDPOINT;



    private static final String SNOWY_FILE_ALIYUN_ACCESS_KEY_ID_KEY = "SNOWY_FILE_ALIYUN_ACCESS_KEY_ID";
    private static final String SNOWY_FILE_ALIYUN_ACCESS_KEY_SECRET_KEY = "SNOWY_FILE_ALIYUN_ACCESS_KEY_SECRET";
    private static final String SNOWY_FILE_ALIYUN_ROLE_SESSION_NAME = "SNOWY_FILE_ALIYUN_VOD_ROLE_SESSION_NAME";
    private static final String SNOWY_FILE_ALIYUN_VOD_ROLE_ARN = "SNOWY_FILE_ALIYUN_VOD_ROLE_ARN";
    private static final String SNOWY_FILE_ALIYUN_VOD_POLICY = "SNOWY_FILE_ALIYUN_VOD_POLICY";
    private static final String SNOWY_FILE_ALIYUN_VOD_STS_ENDPOINT = "SNOWY_FILE_ALIYUN_VOD_STS_ENDPOINT";

    private static Client client;

    private static void initStatic() throws Exception {
        DevConfigApi devConfigApi = SpringUtil.getBean(DevConfigApi.class);

        /* ALIYUN_ACCESS_KEY_ID_KEY */
        ALIYUN_ACCESS_KEY_ID_KEY = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_ACCESS_KEY_ID_KEY);
        if (ObjectUtil.isEmpty(ALIYUN_ACCESS_KEY_ID_KEY)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：accessKeyId为空");
        }

        /* ALIYUN_ACCESS_KEY_SECRET_KEY */
        ALIYUN_ACCESS_KEY_SECRET_KEY = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_ACCESS_KEY_SECRET_KEY);
        if (ObjectUtil.isEmpty(ALIYUN_ACCESS_KEY_SECRET_KEY)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：accessKeySecret为空");
        }


        /* SNOWY_FILE_ALIYUN_ROLE_SESSION_NAME */
        ALIYUN_ROLE_SESSION_NAME = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_ROLE_SESSION_NAME);
        if (ObjectUtil.isEmpty(ALIYUN_ROLE_SESSION_NAME)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：ALIYUN_ROLE_SESSION_NAME为空");
        }

        /* SNOWY_FILE_ALIYUN_VOD_ROLE_ARN */
        ALIYUN_VOD_ROLE_ARN = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_VOD_ROLE_ARN);
        if (ObjectUtil.isEmpty(ALIYUN_VOD_ROLE_ARN)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：SNOWY_FILE_ALIYUN_VOD_ROLE_ARN为空");
        }

        /* SNOWY_FILE_ALIYUN_VOD_POLICY */
        ALIYUN_VOD_POLICY = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_VOD_POLICY);
        if (ObjectUtil.isEmpty(ALIYUN_VOD_POLICY)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：SNOWY_FILE_ALIYUN_VOD_POLICY为空");
        }

        /* SNOWY_FILE_ALIYUN_VOD_STS_ENDPOINT */
        ALIYUN_VOD_STS_ENDPOINT = devConfigApi.getValueByKey(SNOWY_FILE_ALIYUN_VOD_STS_ENDPOINT);
        if (ObjectUtil.isEmpty(ALIYUN_VOD_STS_ENDPOINT)) {
            throw new CommonException("阿里云文件操作客户端未正确配置：SNOWY_FILE_ALIYUN_VOD_STS_ENDPOINT为空");
        }


        Config config = new Config()
                // 从环境变量中获取AccessKey ID的值
                .setAccessKeyId(ALIYUN_ACCESS_KEY_ID_KEY)
                // 从环境变量中获取AccessKey Secret的值
                .setAccessKeySecret(ALIYUN_ACCESS_KEY_SECRET_KEY);
        config.endpoint = ALIYUN_VOD_STS_ENDPOINT;
          client = new Client(config);


    }


    public static JSONObject stsInfo() throws Exception {
        initStatic();
        // 从环境变量中获取步骤一生成的RAM用户的访问密钥（AccessKey ID和AccessKey Secret）。
        AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest()
                .setRoleArn(ALIYUN_VOD_ROLE_ARN)
                .setRoleSessionName(ALIYUN_ROLE_SESSION_NAME)
                .setPolicy(ALIYUN_VOD_POLICY);
        RuntimeOptions runtime = new RuntimeOptions();

        AssumeRoleResponse assumeRoleResponse = client.assumeRoleWithOptions(assumeRoleRequest, runtime);
        return JSONUtil.parseObj(assumeRoleResponse.body.credentials);


    }

}
